<script setup>
import { LoginAPI } from '@/apis/user';
import router from '@/router';
import { useUserStore } from '@/stores';
import { ElMessage } from 'element-plus';
const ruleForm = ref({
  username: '',
  password: '',
})
const rules = {
  username: [{ required: true, message: 'username is required' }],
  password: [{ required: true, message: 'password is required' }],
}

const sutmitForm = async () => {
  const result = await LoginAPI(ruleForm.value)
  const userStore = useUserStore()
  userStore.setUserInfo(result)
  ElMessage.success("登陆成功")
  setTimeout(() => {
    router.push('/')
  }, 300)
}
</script>

<template>
  <div class="login">
    <div class="form">
      <StCrad>
        <el-form
          ref="ruleFormRef"
          style="max-width: 600px"
          :model="ruleForm"
          status-icon
          :rules="rules"
          label-width="auto"
          class="demo-ruleForm"
        >
          <el-form-item label="账号" prop="username">
            <el-input v-model="ruleForm.username" type="text" autocomplete="off" />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="ruleForm.password" type="password" autocomplete="off" />
          </el-form-item>
          <el-form-item >
            <div class="btn">
              <el-button type="primary" @click="sutmitForm"> 登录 </el-button>
              <el-button >重置</el-button>
            </div>
          </el-form-item>
        </el-form>
      </StCrad>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100vh;
  background: url(../../assets/book.jpg) no-repeat center/cover;
}

.form {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(10%, -50%);
  width: 400px;
}
.btn {
  width: 100%;
  display: flex;
  justify-content: center;
}
</style>
